/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkListBox.
	/// </summary>
	public class ResLinkListBox : ResLinkBaseTemplatedWebControl
	{
		public ResLinkListBox()
		{

		}

		public CA.TreeView Tree;

		protected override void AttachChildControls()
		{
			Tree = FindControl("ResLinkTree") as CA.TreeView;
		}

		public override void DataBind()
		{
			base.DataBind();

			BuildSectionTree();
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);

			if(!Page.IsPostBack)
			{
				DataBind();
			}
		}

		private CA.TreeViewNode BuildNode(string name)
		{
			CA.TreeViewNode node = new CA.TreeViewNode();

			if (name.Length > 30)
				node.Text = name.Substring(0, 27) + "...";
			else
				node.Text = name;

			return node;
		}


		private CA.TreeViewNode BuildRootnode()
		{
			CA.TreeViewNode node = new CA.TreeViewNode();
			node.Text = "Root";
			node.DraggingEnabled = false;
			node.DroppingEnabled = false;
			node.EditingEnabled = false;
			node.ImageUrl = "folder.gif";
			node.Value = "-1";
			node.Expanded = true;
			node.ID = "-1";
			return node;
		}


		private void BuildSectionTree()
		{
			if (Tree == null)
				return;

			// Create root node
			CA.TreeViewNode rootNode = BuildRootnode();
			Tree.Nodes.Add(rootNode);
			
			BuildSectionTree(-1 , rootNode.Nodes);
		}


		private int BuildSectionTree(int sectionID, CA.TreeViewNodeCollection nodes)
		{
			ArrayList sections = ResLinkSection.GetSection(sectionID).Sections;

			CA.TreeViewNode node;
			foreach (Section sec in sections)
			{
				// Get a list of sections in this group
				node = BuildNode(sec.Name);
				node.DraggingEnabled = false;
				node.DroppingEnabled = false;
				node.EditingEnabled = false; 
				node.ID = String.Format("{0}", sec.SectionID);
				node.Value = String.Format("{0}", sec.SectionID);

				//If the user has access, even if the section is not active still want to be able to move it
				if (ResLinkPermission.LightAccessCheck((ResLinkSection)sec , (Permission)ResLinkPermissionEnum.AddLinks , CSContext.Current.User))
				{
					node.ImageUrl = "folder.gif";
					node.Checked = true;
				}
				else
				{
					node.ImageUrl = "deletedFolder.gif";
					node.Checked = false;
				}

				nodes.Add(node);

				if (sec.SectionID == CSContext.Current.SectionID)
				{
					node.Expanded = true;
					if (CSContext.Current.SectionID < 0)
					{
						node.CssClass = Tree.SelectedNodeCssClass;
						node.HoverCssClass = Tree.SelectedNodeCssClass;
					}

				}

				int added = BuildSectionTree(sec.SectionID, node.Nodes);
			}

			return sections.Count;
		}
	}
}
